/**
 * Created by Syaoran on 21/11/30.
 * 错误捕获模块
 */
import Vue from "vue";

const state = {
  logs: []
};

const mutations = {
  ADD_ERROR_LOG: (state, log) => {
    state.logs.push(log);
  },
  CLEAR_ERROR_LOG: (state) => {
    state.logs = [];
  }
};

const actions = {
  /* 添加一条错误信息 */
  addErrorLog ({ commit }, log) {
    commit("ADD_ERROR_LOG", log);

    /* 仅在开发环境下打开错误提示 */
    if (process.env.NODE_ENV === "development") {
      Vue.prototype.$mtNotification({
        title: "前端报错",
        type: "error",
        message: log.err
      });
    }
  },

  /* 清空错误信息 */
  clearErrorLog ({ commit }) {
    commit("CLEAR_ERROR_LOG");
  }
};

export default {
  namespaced: true,
  state,
  mutations,
  actions
};
